package com.lepu.pcecg500.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class OpenHelper extends SQLiteOpenHelper {

    private static final String TAG = "OpenHelper";

    private DataBaseContext mContext ;
    private static SQLiteDatabase INSTANCE;

    public SQLiteDatabase getInstance(){
        if (INSTANCE == null) {
            INSTANCE = new OpenHelper(mContext).getDb();
        }
        return INSTANCE;
    }

    public OpenHelper(Context context){
        super(context, Constant.DATABASE_NAME,null,Constant.DATABASE_VERSION);
        this.mContext = new DataBaseContext(context);
    }

    /**
     *
     * @param context 上下文对象
     * @param name 创建数据库名字
     * @param factory 工厂
     * @param version 版本
     */
    //构造函数
    public OpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    /*
    创建数据库时使用的函数
     */
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql = "create table "+Constant.TABLE_NAME+"("+
                Constant.ID+" Integer primary key AUTOINCREMENT,"+
                Constant.Code+" varchar(255),"+
                Constant.Content+" varchar(255),"+
                Constant.State+" varchar(255),"+
                Constant.Type+" varchar(255),"+
                Constant.ADDDATE+" varchar(255),"+
                Constant.ADDTime+" varchar(255))";
        sqLiteDatabase.execSQL(sql);
        String sql1 = "create table "+Constant.TABLE1_NAME2+" ("+
                Constant.ID+" Integer primary key AUTOINCREMENT,"+
                Constant.Code+" varchar(255),"+
                Constant.Content+" varchar(255),"+
                Constant.Content2+" varchar(255),"+
                Constant.Content3+" varchar(255),"+
                Constant.State+" varchar(255),"+
                Constant.Type+" varchar(255),"+
                Constant.ADDDATE+" varchar(255),"+
                Constant.ADDTime+" varchar(255))";
        sqLiteDatabase.execSQL(sql1);
        String sql2 = "create table "+Constant.TABLE1_NAME3+" ("+
                Constant.ID+" Integer primary key AUTOINCREMENT,"+
                Constant.Code+" varchar(255),"+
                Constant.Content+" varchar(255),"+
                Constant.Content2+" varchar(255),"+
                Constant.Content3+" varchar(255),"+
                Constant.Content4+" varchar(255),"+
                Constant.Content5+" varchar(255),"+
                Constant.Content6+" varchar(255),"+
                Constant.Content7+" varchar(255),"+
                Constant.Content8+" varchar(255),"+
                Constant.State+" varchar(255),"+
                Constant.Type+" varchar(255),"+
                Constant.ADDDATE+" varchar(255),"+
                Constant.ADDTime+" varchar(255))";
        sqLiteDatabase.execSQL(sql2);
        Log.e("SQL","数据库被创建了");
    }
    /**
     *  更新数据库时调用
     * @param sqLiteDatabase
     * @param i
     * @param i1
     */
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {

        Log.e(TAG, "onUpgrade: oldVersion " + oldVersion + " newVersion " + newVersion );
    }

    /**获取数据库路径**/
    public String getDBPath(){
        return mContext.getDatabasePath(Constant.DATABASE_NAME).getAbsolutePath();
    }

    public SQLiteDatabase getDb() {
        return SQLiteDatabase.openOrCreateDatabase(getDBPath(), null);
    }

}